package org.kymjs.emoji;

import org.kymjs.emoji.control.EmojiPagerAdapter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioGroup;

public class KJEmojiFragment extends Fragment {

    public static final int COUNT_IN_PAGE = 20; // 每页显示多少个表情(要减去一个删除符号:例如这里是三行七列)
    public static final int COLUMNS = 7; // 每页显示多少列

    private ViewPager mEmojiPager;
    private View mRootView;

    private View[] mEmojiTabs;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        mRootView = inflater.inflate(R.layout.frag_main, container, false);
        // 尽管这里先init控件后init数据更直观，但是为了符合KJFrameForAndroid规范，方便他人阅读，先初始化数据后初始化控件
        initData(mRootView);
        initWidget(mRootView);
        return mRootView;
    }

    private void initData(View rootView) {
        RadioGroup bottomGroup = (RadioGroup) rootView
                .findViewById(R.id.emoji_bottom);
        int count = bottomGroup.getChildCount();
        mEmojiTabs = new View[count];
        if (count > 1) {
        } else {
            // 只有一个分类的时候就不显示了
            bottomGroup.setVisibility(View.GONE);
        }
        for (int i = 0; i < count; i++) {
            mEmojiTabs[i] = bottomGroup.getChildAt(i);
            mEmojiTabs[i].setOnClickListener(getBottomBarClickListener(i));
        }
    }

    private void initWidget(View rootView) {
        mEmojiPager = (ViewPager) rootView.findViewById(R.id.emoji_pager);
        mEmojiPager.setAdapter(new EmojiPagerAdapter(getFragmentManager(), 1));
    }

    /**
     * 底部栏点击事件监听器
     * 
     * @param index
     * @return
     */
    private OnClickListener getBottomBarClickListener(final int index) {
        return new OnClickListener() {
            @Override
            public void onClick(View v) {
                mEmojiPager.setAdapter(new EmojiPagerAdapter(
                        getFragmentManager(), index));
            }
        };
    }
}
